import { Options, Vue } from 'vue-class-component'
import navigator from '@/components/common/navigator.vue'
import router from '@/router'
import store from '@/store'
import { ElMessage } from 'element-plus'

interface IRealNameAuthentication {
  userId: string
  realName: string
  idCardNumber: string
}

interface IPurse {
  userId: string
  userBalance: number
  walletId: number
  openTime: Date
}

@Options({
  components: {
    navigator
  }
})

export default class purse extends Vue {
  userId = '0'
  realStat
  dialogFormVisible = false
  transactionRecord (): void {
    router.push({
      path: '/transactionRecord'
    })
  }

  realNameAuthentication: IRealNameAuthentication = {
    userId: '',
    realName: '',
    idCardNumber: ''
  }

  purseParams: IPurse = {
    userId: '',
    userBalance: 0,
    walletId: 0,
    openTime: new Date()
  }

  mounted () {
    if (store.state.u !== null) {
      this.userId = store.state.u.userId
      this.realNameAuthentication.userId = this.userId
      store.dispatch('GetRealStat', this.userId)
        .then(response => {
          this.realStat = response.data.data
          if (this.realStat === 0) {
            this.dialogFormVisible = true
          } else {
            store.dispatch('selectUserBalance', this.userId)
              .then(response => {
                this.purseParams.userBalance = response.data.data
                console.log(this.purseParams)
              }).catch(() => {
                console.log('connect failed')
              })
          }
        }).catch(() => {
          console.log('connect failed')
        })
    }
  }

  realName () {
    store.dispatch('RealNameAuthentication', this.realNameAuthentication)
      .then(response => {
        const code = response.data.code
        const message = response.data.message
        if (code === 200) {
          ElMessage.success(message)
          this.dialogFormVisible = false
        } else {
          ElMessage.warning(message)
        }
      }).catch(() => {
        console.log('connect failed')
      })
  }

  cancel () {
    this.dialogFormVisible = false
    router.push('/basicInformation')
  }
}
